#include "types.h"
#include "error_insert_help.h"
#include "set_error_insert.h"

#include <stdio.h>

int main(int argc, char** argv)
{
	int tmp;
	int ret = scanf("%x", &tmp);
	byte xor_error = tmp;
	error_struct error_data;
	error_data.size = sizeof(error_data);
	error_data.byte_pos = 0;
	error_data.round = 15;
	error_data.error_byte = xor_error;
	error_data.apply_func = xor_error_apply;


	if(!add_error_struct(&error_data))
	{
		printf("add_error_struct error!\n");
		return 1;
	}
	
	byte plaintext[BLOCK_SIZE] = {
		0
	};
	byte key[16] = {
		0
	};
	byte cipher[BLOCK_SIZE];
	byte error_cipher[BLOCK_SIZE];
	key_type encrypt_key;
	int i;
	printf("Plaintext:\t");
	for( i = 0; i < BLOCK_SIZE; ++i)
	{
		printf("%.2x", plaintext[i]);
	}
	printf("\n");
	
	pre_expand_key(key, sizeof(key), &encrypt_key);

	set_error_insert(NULL);
	twofish_128_encrypt(plaintext, cipher, &encrypt_key);

	set_error_insert(error_insert_hook_func);
	twofish_128_encrypt(plaintext, error_cipher, &encrypt_key);

	printf("Ciphertext:\t");
	for( i = 0; i < BLOCK_SIZE; ++i)
	{
		printf("%.2x", cipher[i]);
	}
	printf("\n");
	
	printf("Ciphertext_e:\t");
	for( i = 0; i < BLOCK_SIZE; ++i)
	{
		printf("%.2x", error_cipher[i]);
	}
	printf("\n");

	printf("Xor result:\t");
	for( i = 0; i < BLOCK_SIZE; ++i)
	{
		printf("%.2x", cipher[i] ^ error_cipher[i]);
	}
	printf("\n");
}
